package com.example.fasttalk.window;

import com.example.fasttalk.client.MessagesManagerClient;
import com.example.fasttalk.model.FastTalkGroupModel;
import com.example.fasttalk.model.FastTalkRecordModel;
import com.example.fasttalk.model.FastTalkUserModel;
import com.intellij.openapi.util.text.Strings;
import com.intellij.ui.CollectionListModel;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.ui.components.JBTextField;
import com.intellij.util.ui.components.BorderLayoutPanel;
import java.awt.event.AdjustmentEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;

/**
 * @author SundayLi
 * @date 2023/3/2 17:30
 */
public class FastTalkMainWindow extends BorderLayoutPanel {
  private FastTalkGroupModel fastTalkGroupModel;
  private FastTalkUserModel fastTalkUserModel;
  private FastTalkWindowManager fastTalkWindowManager;


  private JScrollPane scrollPane;
  private JList<FastTalkRecordViewWindow> recordList;
  private JBTextField talkTextField;
  private JButton emoticonBtn;
  private JButton sendTalkBtn;
  private JPanel operationPanel;
  private CollectionListModel<FastTalkRecordViewWindow> recordModelList;
  private AtomicBoolean isBottoming;

  FastTalkMainWindow() {
    super();
  }

  FastTalkMainWindow(FastTalkGroupModel fastTalkGroupModel, FastTalkUserModel fastTalkUserModel, FastTalkWindowManager fastTalkWindowManager) {
    this();
    this.fastTalkGroupModel = fastTalkGroupModel;
    this.fastTalkUserModel = fastTalkUserModel;
    this.fastTalkWindowManager = fastTalkWindowManager;
    init();
    initListener();
  }

  private void initListener() {
    sendTalkBtn.addActionListener(e -> {
      String sendText = talkTextField.getText();
      if (Strings.isNotEmpty(sendText)) {
        MessagesManagerClient.getInstance().sendTalkText(sendText, fastTalkGroupModel);
        talkTextField.setText("");
      }
    });
    talkTextField.addKeyListener(new KeyAdapter(){
      @Override
      public void keyPressed(KeyEvent e)
      {
        if(e.getKeyChar()==KeyEvent.VK_ENTER )
        {
          sendTalkBtn.doClick();
        }
      }
    });
    scrollPane.getVerticalScrollBar().addAdjustmentListener(evt -> {
      if(evt.getAdjustmentType() == AdjustmentEvent.TRACK && !isBottoming.get()) {
        JScrollBar jscrollBar = scrollPane.getVerticalScrollBar();
        jscrollBar.setValue(jscrollBar.getMaximum());
        isBottoming.set(true);
      }
    });
    MessagesManagerClient.getInstance().addTalkListener(tTalkRecordInfo -> {
      if (tTalkRecordInfo.getGroupId() == fastTalkGroupModel.getId()) {
        FastTalkUserModel userModel = new FastTalkUserModel();
        userModel.setUserName(tTalkRecordInfo.getName());
        FastTalkRecordModel fastTalkRecordModel =
          new FastTalkRecordModel(tTalkRecordInfo.getText(), fastTalkGroupModel, userModel);
        recordModelList.add(new FastTalkRecordViewWindow(fastTalkRecordModel));
        isBottoming.set(false);
      }
    });
    emoticonBtn.addActionListener(e -> {

    });
  }

  private void init() {
    isBottoming = new AtomicBoolean(true);
    emoticonBtn = new JButton();
    emoticonBtn.setText("emoticon");

    talkTextField = new JBTextField();

    sendTalkBtn = new JButton();
    sendTalkBtn.setText("send");

    // ---- 组装操作栏
    operationPanel = new JPanel();
    operationPanel.add(emoticonBtn);
    operationPanel.add(talkTextField);
    operationPanel.add(sendTalkBtn);

    recordModelList = new CollectionListModel<>();
    recordList = new JList<>(recordModelList);
    recordList.setCellRenderer((list, value, index, isSelected, cellHasFocus) -> value);

    scrollPane = new JBScrollPane();
    scrollPane.getViewport().setView(recordList);

    // ---- 组装整个聊天界面
    this.addToCenter(scrollPane);
    this.addToBottom(operationPanel);
  }
}
